package com.liz.rpcserver.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.liz.lizrpcapi.commons.Node;
import com.liz.lizrpcapi.entity.RegisterEntity;
import com.liz.rpcserver.commons.GlobalHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author liangGongWei
 * @description
 * @create 2021-08-23 22:56
 **/
@Slf4j
@Service
public class RegisterService {

    public void register(RegisterEntity entity) {
        String clientId = entity.getClientId();
        String ip = entity.getIp();
        if (StringUtils.isEmpty(ip) || StringUtils.isEmpty(clientId)){
            throw new RuntimeException("参数不全");
        }
        Map<String, List<String>> classMethodNameMap = entity.getClassMethodNameMap();
        for (Map.Entry<String, List<String>> entry : classMethodNameMap.entrySet()) {
            String className = entry.getKey();
            Map<String,Node> nodeMap = GlobalHolder.classNodeMap.computeIfAbsent(className, k -> Maps.newConcurrentMap());
            Node node = nodeMap.computeIfAbsent(clientId, key -> new Node());
            node.setIp(ip);
            node.setMethodList(entry.getValue());
            node.setProtocolPort(entity.getProtocolPort());
        }
    }

    public Map<String, List<Node>> remoteClassNode() {
//        GlobalHolder.classNodeMap
        return null;
    }
}
